#!/bin/bash

# 
# pxcompressimage
# compresses an image, and, if mhd, deletes the uncompressed raw.
#

if [ $# = 0 ]
then
  echo "pxcompressimage"
  echo
  echo "Usage: pxcompressimage <inputfilename1> ..."
  echo
  echo "If the input is an uncompressed mhd, the raw file is deleted."
  echo
  exit 64
fi

# The list of input images
inputimages="$*"

for inputimage in $inputimages
do
  # Compress image
  pxcastconvert -in "$inputimage" -out "$inputimage" -z

  # Check for success
  if [ "$?" != "0" ]
  then
    exit 65
  fi

  # Check for mhd
  ext=`expr match "$inputimage" '.*\([.][mM][hH][dD]\)'`
  ismhd="$?"

  # If so. remove .raw file
  if [ "$ismhd" = "0" ]
  then
    inputimageraw="${inputimage/%$ext/.raw}"
    if [ -e "$inputimageraw" ]
    then
      rm "$inputimageraw"
    fi
  fi
done

# return a value
exit 0

